package com.qzq.weibo.http;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.qzq.weibo.entity.Comment;
import com.qzq.weibo.entity.Status;
import com.qzq.weibo.entity.UnreadCount;
import com.qzq.weibo.entity.User;

public class JsonHelper {

	private static final String TAG = "JsonHelper";

	public User getUserInfo(String json) {
		User user = new User();
		try {
			JSONObject object = new JSONObject(json);

			user.setIdstr(object.getString("idstr"));
			user.setAvatar_hd(object.getString("avatar_hd"));
			user.setAvatar_large(object.getString("avatar_large"));
			user.setDescription(object.getString("description"));
			user.setFollowers_count(object.getInt("followers_count"));
			user.setFriends_count(object.getInt("friends_count"));
			user.setGender(object.getString("gender"));
			user.setLocation(object.getString("location"));
			user.setProfile_image_url(object.getString("profile_image_url"));
			user.setScreen_name(object.getString("screen_name"));
			user.setStatuses_count(object.getInt("statuses_count"));

			String jdate = object.getString("created_at");
			user.setCreated_at(formatDateTime(jdate));
		} catch (JSONException e) {

			Log.d(TAG, "getUserInfo json数据解析出错");
			e.printStackTrace();
		}

		return user;
	}

	public List<User> getUsers(String json) {

		List<User> users = new ArrayList<User>();

		try {
			
			JSONArray jUsers = new JSONObject(json).getJSONArray("users");

			for (int i = 0; i < jUsers.length(); i++) {

				JSONObject jUser = jUsers.getJSONObject(i);

				User user = new User();

				user.setIdstr(jUser.getString("idstr"));
				user.setAvatar_hd(jUser.getString("avatar_hd"));
				user.setAvatar_large(jUser.getString("avatar_large"));
				user.setDescription(jUser.getString("description"));
				user.setFollowers_count(jUser.getInt("followers_count"));
				user.setFriends_count(jUser.getInt("friends_count"));
				user.setGender(jUser.getString("gender"));
				user.setLocation(jUser.getString("location"));
				user.setProfile_image_url(jUser.getString("profile_image_url"));
				user.setScreen_name(jUser.getString("screen_name"));
				user.setStatuses_count(jUser.getInt("statuses_count"));

				String jdate = jUser.getString("created_at");
				user.setCreated_at(formatDateTime(jdate));

				users.add(user);

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return users;

	}

	public List<Status> getStatus(String json) {
		List<Status> statuses = new ArrayList<Status>();
		try {
			JSONArray array = new JSONObject(json).getJSONArray("statuses");
			for (int i = 0; i < array.length(); i++) {
				JSONObject jStatus = array.getJSONObject(i);
				Status status = new Status();
				User user = new User();
				status.setAttitudes_count(jStatus.getInt("attitudes_count"));
				if (jStatus.has("bmiddle_pic")) {
					status.setBmiddle_pic(jStatus.getString("bmiddle_pic"));
				}
				status.setComments_count(jStatus.getInt("comments_count"));
				status.setCreated_at(formatDateTime(jStatus
						.getString("created_at")));
				String favorited = jStatus.getString("favorited");
				if (favorited.equals("true")) {
					status.setFavorited(true);
				} else {
					status.setFavorited(false);
				}
				status.setId(jStatus.getInt("id"));
				status.setIdstr(jStatus.getString("idstr"));
				status.setMid(jStatus.getInt("mid"));

				JSONArray jPic_urls = jStatus.getJSONArray("pic_urls");
				// Log.d(TAG, "jPic_urls :" + jPic_urls.toString());
				List<String> pic_urls = new ArrayList<String>();

				if (!jPic_urls.toString().equals("[]")) {

					for (int j = 0; j < jPic_urls.length(); j++) {
						JSONObject jUrl = jPic_urls.getJSONObject(j);
						String url = jUrl.getString("thumbnail_pic");
						pic_urls.add(url);
					}

					// Log.d(TAG, "有图片");

				}
				status.setPic_urls(pic_urls);

				if (jStatus.has("original_pic")) {
					status.setOriginal_pic(jStatus.getString("original_pic"));
				}
				status.setReposts_count(jStatus.getInt("reposts_count"));
				status.setSource(formatSource(jStatus.getString("source")));
				status.setText(jStatus.getString("text"));
				if (jStatus.has("thumbnail_pic")) {
					status.setThumbnail_pic(jStatus.getString("thumbnail_pic"));
				}
				String truncated = jStatus.getString("truncated");
				if (truncated.equals("true")) {
					status.setTruncated(true);
				} else {
					status.setTruncated(false);
				}

				JSONObject jUser = jStatus.getJSONObject("user");
				user.setAvatar_hd(jUser.getString("avatar_hd"));
				user.setAvatar_large(jUser.getString("avatar_large"));
				user.setScreen_name(jUser.getString("screen_name"));
				status.setUser(user);

				if (jStatus.has("retweeted_status")) {
					JSONObject jRetweeted_status = jStatus
							.getJSONObject("retweeted_status");
					Status retweeted_status = new Status();
					retweeted_status.setText(jRetweeted_status
							.getString("text"));

					User retweeted_status_user = new User();
					JSONObject jRetweeted_status_user = jRetweeted_status
							.getJSONObject("user");
					retweeted_status_user.setScreen_name(jRetweeted_status_user
							.getString("screen_name"));
					retweeted_status.setUser(retweeted_status_user);

					JSONArray jRtweet_pic_urls = jRetweeted_status
							.getJSONArray("pic_urls");
					List<String> retweet_pic_urls = new ArrayList<String>();

					if (!jRtweet_pic_urls.toString().equals("[]")) {

						for (int j = 0; j < jRtweet_pic_urls.length(); j++) {
							JSONObject jRetweet_url = jRtweet_pic_urls
									.getJSONObject(j);
							String retweet_url = jRetweet_url
									.getString("thumbnail_pic");
							retweet_pic_urls.add(retweet_url);
						}

						Log.d(TAG, "有图片");

					}

					retweeted_status.setPic_urls(retweet_pic_urls);

					status.setRetweeted_status(retweeted_status);
				}

				statuses.add(status);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			Log.d(TAG, "Status解析出错");
		}
		return statuses;
	}

	public List<Comment> getComments(String json) {

		List<Comment> comments = new ArrayList<Comment>();
		try {
			JSONArray array = new JSONObject(json).getJSONArray("comments");
			for (int i = 0; i < array.length(); i++) {

				JSONObject jComment = array.getJSONObject(i);
				Comment comment = new Comment();
				comment.setCreated_at(formatDateTime(jComment
						.getString("created_at")));
				comment.setId(jComment.getInt("id"));
				comment.setIdstr(jComment.getString("idstr"));
				comment.setMid(jComment.getString("mid"));
				comment.setSource(formatSource(jComment.getString("source")));
				comment.setText(jComment.getString("text"));
				JSONObject jUser = jComment.getJSONObject("user");
				User user = new User();
				user.setScreen_name(jUser.getString("screen_name"));
				user.setAvatar_large(jUser.getString("avatar_large"));
				user.setAvatar_hd(jUser.getString("avatar_hd"));
				comment.setUser(user);

				comments.add(comment);

			}
		} catch (JSONException e) {
			e.printStackTrace();
			Log.d(TAG, "getComments数据解析出错");
		}
		return comments;

	}

	public UnreadCount getUreadCount(String json) {

		UnreadCount unreadCount = new UnreadCount();
		try {

			JSONObject jUnreadCount = new JSONObject(json);

			unreadCount.setBadge(jUnreadCount.getInt("badge"));
			unreadCount.setCmt(jUnreadCount.getInt("cmt"));
			unreadCount.setDm(jUnreadCount.getInt("dm"));
			unreadCount.setFollower(jUnreadCount.getInt("follower"));
			unreadCount.setGroup(jUnreadCount.getInt("group"));
			unreadCount.setInvite(jUnreadCount.getInt("invite"));
			unreadCount.setMention_cmt(jUnreadCount.getInt("mention_cmt"));
			unreadCount
					.setMention_status(jUnreadCount.getInt("mention_status"));
			unreadCount.setNotice(jUnreadCount.getInt("notice"));
			unreadCount.setPhoto(jUnreadCount.getInt("photo"));
			if (jUnreadCount.has("private_group")) {
				unreadCount.setPrivate_group(jUnreadCount
						.getInt("private_group"));
			}
			unreadCount.setStatus(jUnreadCount.getInt("status"));

		} catch (JSONException e) {
			e.printStackTrace();
			Log.d(TAG, "getUreadCount 数据解析出错");
		}
		return unreadCount;

	}

	public String formatDateTime(String created_at) {

		Date date = new Date();
		SimpleDateFormat sdf1 = new SimpleDateFormat(
				"EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
		try {
			date = sdf1.parse(created_at);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);

		String new_date = sdf2.format(date);
		return new_date;
	}

	public String formatSource(String source) {

		int start = source.indexOf(">") + 1;
		int end = source.indexOf("<", start);

		return source.substring(start, end);
	}
}
